//*************************************************************************//
//*           _____   ____________            ______               __     *//
//*          / /__ \ / ____/ ____/           / ____/___  _________/ /     *//
//*     __  / /__/ // __/ / __/    ______   / /_  / __ \/ ___/ __  /      *//
//*    / /_/ // __// /___/ /___   /_____/  / __/ / /_/ / /  / /_/ /       *//
//*    \____//____/_____/_____/           /_/    \____/_/   \__,_/        *//
//*                                                                       *//
//*     Copyright (c) 2006 Ford Argentina. All Rights Reserved.           *//
//*                                                                       *//
//*************************************************************************//

package ar.com.ford.it.webengine.system.logs;


import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import ar.com.ford.it.webengine.helper.JavaPackage;
import com.ford.it.logging.ILogger;
import com.ford.it.logging.Level;
import com.ford.it.logging.LogFactory;

/**
 * AdminLogsFacade <br>
 * Facade de administracion e logs de la aplicacion
 * 
 * @author Aji
 */
public class AdminLogsFacade {

	private static final String CLASS_NAME = AdminLogsFacade.class.getName();
	private static final ILogger log = LogFactory.getInstance().getLogger(CLASS_NAME);
	

	/**
	 * Enviar a un HttpServletResponse u narchivo zip con los logs de la aplicacion
	 * @param response HttpServletResponse
	 */
	public void getZippedLogs(HttpServletResponse response) {
		String METHOD_NAME = "getZippedLogs";
		
		log.entering(CLASS_NAME, METHOD_NAME);
		LogsCompressor lcomp = new LogsCompressor();
		
		try {
						
			
			response.setContentType("application/x-zip");
			response.setHeader("Content-Disposition","inline; filename=logs.zip");
			
			ServletOutputStream output = response.getOutputStream();
			output.write(lcomp.getZippedLogs());
			output.close();
			

		}
		catch (Exception excep) {
			log.logp(Level.SEVERE, CLASS_NAME, METHOD_NAME, "Exception: ", excep);
		}
		
		log.exiting(CLASS_NAME, METHOD_NAME);
		
	}

	/**
	 * Obtener una collection con las clases de un package dado
	 * @param pkg Package
	 * @return Collection con las clases de un package dado 
	 */
	public Collection getClassesFromPackage(String pkg) {

		JavaPackage jp = new JavaPackage(pkg, false);
		
		return jp.getClassesCollection();
	}
    
	/**
	 * Obtener packages de la aplicacion
	 * @return List de packages de la aplicacion
	 */
	public List getApplicationPackages() {
		JavaPackage javapack = new JavaPackage("ar.com.ford.it", true);

		ArrayList result = new ArrayList();

		buildPackagesList(javapack.getChildrens(), result);
		
		return result;
	}

	/**
	 * Llenar una lista con los packages de la aplicacion (recursivamente)
	 * @param rootList Lista que se recorrera
	 * @param result lista que se llenara con los packages de la aplicacion.
	 */
	private void buildPackagesList(List rootList, ArrayList result) {
		if (rootList != null) {
			Iterator it = rootList.iterator();
			while (it.hasNext()) {
				JavaPackage jp = (JavaPackage)it.next();
				result.add(jp.getName());
				buildPackagesList(jp.getChildrens(), result);
			}
		}
	}

    /**
     * Modificar el nivel de log de una clase especifica
     * @param className Clase
     * @param level Nivel de log: ERROR | INFO | DEBUG
     */
	public void changeLoggerLevel(String className, String level) {
		String METHOD_NAME = "changeLoggerLevel";
		log.entering(CLASS_NAME, METHOD_NAME);

        log.logp(com.ford.it.logging.Level.FINE, CLASS_NAME, METHOD_NAME, "Changing class '"+className+"' to level "+level);
            
  		Logger.getLogger(className).setLevel(org.apache.log4j.Level.toLevel(level, org.apache.log4j.Level.INFO));

		log.exiting(CLASS_NAME, METHOD_NAME);
	}	

}
